<?php

/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/3/3
 * Time: 下午6:47
 */
namespace App\Model\Friend;

use App\Model\Model;
use App\Utility\Common;
use App\Model\User\UserModel;

class FriendModel extends Model
{

    const IS_ONLONE = 1;

    const NOT_ONLONE = 2;

    public function addUser(FriendBean $friend)
    {
        $this->getModel();
        $re = $this->dbConnector()->insert($this->prefix . 'friend', $friend->objToArray(true));
        $this->free();
        return $re;
    }

    public function getAddLog($fuid, $muid): FriendBean
    {
        $this->getModel();
        $db = $this->dbConnector();
        $db = $db->where("fuid", $fuid);
        $db = $db->where("muid", $muid);
        $db = $db->where("ispass", FriendBean::ADD_FRIEND_ING);
        $re = $db->getone($this->prefix . 'friend');
        $this->free();
        return (new FriendBean($re));
    }

    function isFriend($fuid, $muid): FriendBean
    {
        $this->getModel();
        $db = $this->dbConnector();
        $db = $db->where("((muid = ? and fuid = ?) or (muid = ? and fuid = ?)) and ispass  = ?", Array(
            $muid,
            $fuid,
            $fuid,
            $muid,
            FriendBean::ADD_FRIEND_PASS
        ));
        $re = $db->getone($this->prefix . 'friend');
        $this->free();
        return (new FriendBean($re));
    }

    public function getFriends($muid): array
    {
        $this->getModel();
        $db = $this->dbConnector();
        $db = $db->where("(muid = ? and ispass = ?) or( fuid = ? and ispass = ?)", Array(
            $muid,
            FriendBean::ADD_FRIEND_PASS,
            $muid,
            FriendBean::ADD_FRIEND_PASS
        ));
        $re = $db->get($this->prefix . 'friend');
        $this->free();
        if ($re) {
            foreach ($re as $k => $v) {
                if ($v['fuid'] == $muid) {
                    $m = $v['muid'];
                    $f = $v['fuid'];
                    $re[$k]['muid'] = $f;
                    $re[$k]['fuid'] = $m;
                }
                $tofd = (new UserModel())->getFdByUid($re[$k]['fuid']);
                if ((new Common())->isOnline($tofd)) {
                    $re[$k]['isonline'] = self::IS_ONLONE;
                } else {
                    $re[$k]['isonline'] = self::NOT_ONLONE;
                }
                $finfo = (new UserModel())->getUserByUid($re[$k]['fuid']);
                $finfo->setPassword("");
                $re[$k]['finfo'] = $finfo->objToArray(true);
                unset($finfo);
            }
        }
        return $re;
    }

    public function getFriendUids($muid): array
    {
        $this->getModel();
        $db = $this->dbConnector();
        $db = $db->where("(muid = ? and ispass = ?) or( fuid = ? and ispass = ?)", Array(
            $muid,
            FriendBean::ADD_FRIEND_PASS,
            $muid,
            FriendBean::ADD_FRIEND_PASS
        ));
        $cols = [
            "muid",
            "fuid"
        ];
        $re = $db->get($this->prefix . 'friend', null, $cols);
        $this->free();
        if ($re) {
            foreach ($re as $k => $v) {
                if ($v['fuid'] == $muid) {
                    $m = $v['muid'];
                    $f = $v['fuid'];
                    $re[$k]['muid'] = $f;
                    $re[$k]['fuid'] = $m;
                }
            }
        }
        return $re;
    }

    public function getFriendRequests($muid): array
    {
        $this->getModel();
        $db = $this->dbConnector();
        $db = $db->where("ispass", FriendBean::ADD_FRIEND_ING);
        $db = $db->where("fuid", $muid);
        $re = $db->get($this->prefix . 'friend');
        $this->free();
        if ($re) {
            foreach ($re as $k => $v) {
                $m = $v['muid'];
                $f = $v['fuid'];
                $re[$k]['muid'] = $f;
                $re[$k]['fuid'] = $m;
            }
            $finfo = (new UserModel())->getUserByUid($re[$k]['fuid']);
            $finfo->setPassword("");
            $re[$k]['finfo'] = $finfo->objToArray(true);
            unset($finfo);
        }
        return $re;
    }

    function updateDataById(FriendBean $friend)
    {
        $this->getModel();
        $db = $this->dbConnector();
        $db = $db->where("id", $friend->getId());
        $friend->setId("");
        $re = $db->update($this->prefix . 'friend', $friend->objToArray(true));
        $this->free();
        return $re;
    }

    function getInfoById($id)
    {
        $this->getModel();
        $db = $this->dbConnector();
        $db = $db->where("id", $id);
        $re = $db->getone($this->prefix . 'friend');
        $this->free();
        return (new FriendBean($re));
    }
}